package org.finmanager.statistics;

import com.alee.extended.panel.WebButtonGroup;
import com.alee.laf.button.WebButton;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PieLabelLinkStyle;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.RectangleInsets;
import org.jfree.util.Rotation;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import static org.finmanager.core.Constants.DATE_FORMAT_MONTH;

/**
 * Created by Kelvin on 28/4/2014.
 */

//The UI of the statistic pie chart function
public class CategoryPieChartUI {
	JPanel mainPanel = new JPanel(new BorderLayout(0, 0));;

	//Buttons to allow user to go to current/next/previous month in the middle on top of the chart
	WebButton increaseMonthButton = new WebButton(">");
	WebButton decreaseMonthButton = new WebButton("<");
	WebButton setCurrentMonthButton = new WebButton();

	public CategoryPieChartUI(CategoryPieChartController categoryPieChartController) {
		increaseMonthButton.setActionCommand(CategoryPieChartController.INCREASE_MONTH);
		decreaseMonthButton.setActionCommand(CategoryPieChartController.DECREASE_MONTH);
		setCurrentMonthButton.setActionCommand(CategoryPieChartController.SET_CURRENT_MONTH);
		WebButtonGroup controlButtonGroup = new WebButtonGroup(decreaseMonthButton, setCurrentMonthButton, increaseMonthButton);
		controlButtonGroup.setButtonsDrawFocus(false);
		JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		controlPanel.add(controlButtonGroup);

		ChartPanel chartPanel = new ChartPanel(createPieChart(categoryPieChartController.getCategoryPieChartDataSet()));
		mainPanel.add(controlPanel, BorderLayout.NORTH);
		mainPanel.add(chartPanel, BorderLayout.CENTER);

		setCurrentMonthText(Calendar.getInstance());
	}

	public void addButtonListener(ActionListener listener) {
		increaseMonthButton.addActionListener(listener);
		decreaseMonthButton.addActionListener(listener);
		setCurrentMonthButton.addActionListener(listener);
	}

	//showing the month-year inside the button
	public void setCurrentMonthText(Calendar calendar) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_MONTH);
		setCurrentMonthButton.setText(dateFormat.format(calendar.getTime()));
	}

	//Manipulating the items to create the pie chart
	private JFreeChart createPieChart(PieDataset dataset) {
		JFreeChart chart = ChartFactory.createPieChart(null, dataset, false, false, false);
		// TODO:
		// chart.setBackgroundPaint(getMainComponent().getBackground());

		PiePlot plot = (PiePlot) chart.getPlot();
		plot.setStartAngle(90);
		plot.setDirection(Rotation.CLOCKWISE);
		plot.setForegroundAlpha(0.5f);
		plot.setShadowPaint(null);
		plot.setOutlineVisible(false);
		plot.setLabelBackgroundPaint(null);
		plot.setLabelShadowPaint(null);
		plot.setLabelOutlinePaint(null);
		plot.setLabelPadding(new RectangleInsets(-3, 5, 0, 5));
		plot.setLabelLinkStyle(PieLabelLinkStyle.STANDARD);
		plot.setSimpleLabels(false);
		plot.setIgnoreZeroValues(true);
		plot.setNoDataMessage("NO DATA");

		// TODO:
		// plot.setBackgroundPaint(getMainComponent().getBackground());

		return chart;
	}

	public JComponent getMainComponent() {
		return mainPanel;
	}
}
